package com.xspace.android.xafcommon.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.R;
import com.xspace.android.xafcommon.view.dialog.ButtonClickListener;
import com.xspace.android.xafcommon.view.rsv.RangeSliderView;


/**
 * Created by liuchen on 2016/11/22.
 */

public class DialogSetTextSize extends Dialog {
    private Context mContext;
    //    private SeekBar mSeekbar;
    private RangeSliderView mSeekbar;
    private RelativeLayout cancelLayout;
    private ISeekbarListener mListener;
    private ButtonClickListener mClickListener;
    private TextView buttonText;
    private static int oldProgress = 50;


    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().setGravity(Gravity.BOTTOM);
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = d.getWidth();
        getWindow().setAttributes(p);
    }

    public DialogSetTextSize(Context context, int theme) {
        super(context, theme);
        mContext = context;
        setContentView(R.layout.dialog_tiaozhengziti);
        findView();
        init();
    }

    @Override
    public void show() {
//        mSeekbar.setProgress(oldProgress);
        super.show();
    }

    private void setmListener(ISeekbarListener mListener, ButtonClickListener buttonClickListener) {
        this.mListener = mListener;
        this.mClickListener = buttonClickListener;
    }

    private void init() {
//        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
//        layoutParams.width = AppUtils.getDisplayMetrics((Activity) mContext).widthPixels;
//        getWindow().setAttributes(layoutParams);
//        mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                LogUtil.d("progress >>" + progress + " fromUser " + fromUser);
//                if (mListener != null) {
//                    mListener.onProgressChanged(seekBar, progress, fromUser);
//                }
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//                LogUtil.d("begin");
//                if (mListener != null) {
//                    mListener.onStartTrackingTouch(seekBar);
//                }
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//                oldProgress = seekBar.getProgress();
//                LogUtil.d(oldProgress + "  ---  ");
//                if (mListener != null) {
//                    mListener.onStopTrackingTouch(seekBar);
//                }
//
//                dismiss();
//            }
//        });
        mSeekbar.setOnSlideListener(new RangeSliderView.OnSlideListener() {
            @Override
            public void onSlide(int index) {
                if (mListener != null) {
                    mListener.onProgressChanged(mSeekbar, index);
                }
            }
        });
        cancelLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClickListener != null) {
                    mClickListener.buttonClick(v);
                }
                dismiss();
            }
        });
    }

    private void findView() {
//        mSeekbar = (SeekBar) findViewById(R.id.seekBar);
        mSeekbar = (RangeSliderView) findViewById(R.id.rsv_seek);
        cancelLayout = (RelativeLayout) findViewById(R.id.layout_button);
        buttonText = (TextView) findViewById(R.id.tv_button);
    }

    private void setIndex(int index) {
        mSeekbar.setInitialIndex(index);
    }

    private void setButtontext(String text) {
        buttonText.setText(text);
    }

    private void setButtontext(int textRes) {
        buttonText.setText(textRes);
    }

    public static class Builder {
        DialogSetTextSize mDialog;

        /**
         * Constructor using a context for this builder and the {@link AlertDialog} it creates.
         */
        public Builder(Context context, int theme) {
            mDialog = new DialogSetTextSize(context, theme);
        }

        public Builder(Context context) {
            this(context, R.style.CustomDialog);
        }

        public void setListener(ISeekbarListener listener, ButtonClickListener buttonClickListener) {
            mDialog.setmListener(listener, buttonClickListener);
        }

        public void show() {
            mDialog.show();
            mDialog.setCancelable(true);
            mDialog.setCancelable(true);
        }

        public void setIndex(int index) {
            mDialog.setIndex(index);
        }

        public void setButtonText(String text) {
            mDialog.setButtontext(text);
        }

        public void setButtonText(int textRes) {
            mDialog.setButtontext(textRes);
        }

        public void setDismissListener(OnDismissListener dismissListener) {
            mDialog.setOnDismissListener(dismissListener);
        }

        public void setCancelListener(OnCancelListener cancelListener) {
            mDialog.setOnCancelListener(cancelListener);
        }
    }

    public interface ISeekbarListener {

        void onProgressChanged(RangeSliderView rsv, int index);

//        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser);
//
//        public void onStartTrackingTouch(SeekBar seekBar);
//
//        public void onStopTrackingTouch(SeekBar seekBar);
    }
}
